package com.cedifie.lendapp.ui.user.impl

import android.content.Context
import com.cedifie.http.callback.StringCallback
import com.cedifie.http.model.Response
import com.cedifie.lendapp.MyApplication
import com.cedifie.lendapp.R
import com.cedifie.lendapp.http.IHttpCallback
import com.cedifie.lendapp.mvp.presenter.MvpPresenter
import com.cedifie.lendapp.ui.user.view.ICedUserFeedbackView
import com.lib.base.utils.log.LogHelper
import org.json.JSONObject

/**
 * Cedifie
 * @Author wwdeng
 * @CreateTime: 2024/6/14 16:26
 * @Annotation：意见反馈 Presenter 实现
 */
class CedUserFeedbackPresenterImpl(context: Context?) : MvpPresenter<ICedUserFeedbackView>(context) {

    private var feedbackModel: CedUserFeedbackModelImpl? = null
    //反馈类型列表
    private var typeList: ArrayList<JSONObject>? = null

    init {
        feedbackModel = CedUserFeedbackModelImpl()
    }

    /**
     * 获取意见反馈类型
     */
    fun loadQuestionTypeListAction() {
        if (typeList == null) {
            typeList = arrayListOf()
        }
        typeList?.clear()
        val dataList = arrayListOf(
            "Product Suggestion,SUGGEST",
            "App bugs,BUG",
            "Payment issues,PAYMENT",
            "Others,OTHER"
        )
        for (i in 0..<dataList.size) {
            val items = dataList[i].split(",")
            val obj = JSONObject()
            obj.put("title", items[0])
            obj.put("value", items[1])
            obj.put("isSelect", false)
            typeList?.add(obj)
        }
        typeList?.let {
            mvpView?.setCedQuestionTypeList(it)
        }
    }

    /**
     * 图片上传
     */
    fun uploadCedImageAction(imgPath: String){
        mvpView?.showCedLoading(MyApplication.app.getString(R.string.loading))
        feedbackModel?.uploadCedImageAction(imgPath,object : StringCallback() {
            override fun onSuccess(response: Response<String>?) {
                when(response?.code()){
                    200 -> {
                        val data = JSONObject(response.body())
                        when(data.optInt("code")){
                            0 -> {//成功
                                LogHelper.e("image upload success ：$data")
                                val obj = data.optJSONObject("obj")
                                obj?.let {
                                    val imgUrl = it.optString("url")
                                    mvpView?.showCedUploadImageSuccess(imgUrl)
                                }
                            }
                            else -> {
                                mvpView?.showCedToast(data.optString("msg"))
                            }
                        }
                    }
                    else -> {
                        response?.let {
                            mvpView?.showCedToast(it.message())
                        }
                    }
                }
            }
            override fun onError(response: Response<String>?) {
                super.onError(response)
                mvpView?.showCedToast(response?.message())
            }

            override fun onFinish() {
                mvpView?.hideCedLoading()
            }
        })
    }

    /**
     * 提交反馈
     */
    fun submitCedFeedbackAction(params: JSONObject){
        feedbackModel?.submitCedFeedbackAction(params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                val msg = data.optString("msg")
                mvpView?.showCedToast(msg)
                when(data.optInt("code")) {
                    0 -> {//成功
                        mvpView?.showCedSubmitSuccess()
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                mvpView?.showCedToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                mvpView?.hideCedLoading()
            }
        })
    }

    /**
     * 获取全部问题类型列表
     */
    fun getTypeList(): ArrayList<JSONObject>{
        typeList?.let {
            return it
        }
        return ArrayList()
    }
}